package com.cms.recursoshumanos.component.dialog;

import java.util.Calendar;
import java.util.Date;

import com.example.frontendrecursoshumanos.R;
import com.cms.recursoshumanos.utilities.Tool;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;

public class CustomDatePickerDialog extends Dialog 
{

    private DatePicker mSpinner;
    private Date fecha_seleccionada;
    private Date min_date;

    public interface DateDialogListener 
  	{
        public void ready(Date fecha_selec);
        public void cancelled();
    }
 
    private DateDialogListener mReadyListener;

    public CustomDatePickerDialog(Context context, Date fecha_selec, Date min_date, DateDialogListener readyListener) 
    {
        super(context);
        this.setTitle("Seleccione la Fecha de Reservacion");
        mReadyListener = readyListener;
        this.min_date = min_date;
        this.fecha_seleccionada = fecha_selec;

    }
    
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.elementos_spinner_fecha_picker);
        mSpinner = (DatePicker) findViewById (R.id.dpFecha);
       // ArrayAdapter<String> adapter = new ArrayAdapter<String> (mContext, android.R.layout.simple_spinner_dropdown_item, mList);
        //mSpinner.setAdapter(adapter);

        Calendar cal = Calendar.getInstance();
        cal.setTime(fecha_seleccionada);
        mSpinner.setMinDate(min_date.getTime());
        mSpinner.updateDate(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH),cal.get(Calendar.DAY_OF_MONTH));
        
        
        Button buttonOK = (Button) findViewById(R.id.dialogOK);
        Button buttonCancel = (Button) findViewById(R.id.dialogCancel);
        buttonOK.setOnClickListener(new android.view.View.OnClickListener()
        {
            public void onClick(View v) 
            {
                int day = mSpinner.getDayOfMonth();
            	int month = mSpinner.getMonth();
                int year = mSpinner.getYear();
                mReadyListener.ready(Tool.getInstance().getFechaFormato(day, month, year));
                CustomDatePickerDialog.this.dismiss();
            }
        });
        buttonCancel.setOnClickListener(new android.view.View.OnClickListener(){
            public void onClick(View v) 
            {
                mReadyListener.cancelled();
                CustomDatePickerDialog.this.dismiss();
            }
        });
    }
}